<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/20 11:22
 */

namespace app\api\logic;

class Wechatcode
{
    //获取access_token
    public function get_access_token(){
        
        $appid = config("wx.app_id");
        $secret = config("wx.app_secret");
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        return $data = $this->curl_get($url);
    }

    public function curl_get($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }

    //获得二维码
    public function get_qrcode($circle_type,$goods_id) {

        //header('content-type:image/gif');
        //header('content-type:image/png');格式自选，不同格式貌似加载速度略有不同，想加载更快可选择jpg
        //header('content-type:image/jpg');
        $uid = 6;
        $data = array();
        $data['scene'] = "circle_type=".$circle_type."&goods_id=".$goods_id;
        if($circle_type=='1'){
            $data['page'] = "pages/CommodityDetails/CommodityDetails";
        }else{
            $data['page'] = "pages/demandDetails/demandDetails";
        }
        $data = json_encode($data);
        $access = json_decode($this->get_access_token(),true);
        $access_token= $access['access_token'];
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;

        $da = $this->get_http_array($url,$data);
      
        //这里强调显示二维码可以直接写该访问路径，同时也可以使用curl保存到本地，详细用法可以加群或者加我扣扣
        return $da;
    }


    public function get_http_array($url,$post_data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出，不用print_r();也会在后面多个1
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);

        //$out = json_decode($output);

        return $output;
    }

}